//
//  ViewController.m
//  25-多线程
//
//  Created by 王道林 on 16/8/17.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imagevie;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //iOS 下地多线程
    // 1. 进程和线程
    // 什么是进程?   进程是:当用户下达执行应用程序的命令时, 所启动的东西. 进程比作工厂
    // 什么是线程?  线程是进程的执行单位,线程比作工人,
    // 一个进程不能过多的创建线程, 资源消耗过大,  5个左右
    // 注意: 所有UI的操作, 都放在主线程进行
    // 需要开辟线程的操作: 1> 图片文字渲染, 2>大数据存储. 3> 网络请求
    
    // 2. NSThread
    
    //1> 获取到当前的线程<NSThread: 0x7fd95b405c50>{number = 1, name = main}
    // name 线程名  number 线程编号
   NSThread *thread = [NSThread currentThread];
    NSLog(@"%@", thread);
    
    //2> 开辟子线程执行操作
    //    子线程随机创建
    [NSThread detachNewThreadSelector:@selector(newThread) toTarget:self withObject:nil];
    
    // 3> 延迟当前线程的执行..固定的日期
//    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
//    [NSThread sleepUntilDate:date];
//    NSLog(@"1111");
    
    // 4> 相对时间
//    [NSThread sleepForTimeInterval:10];
    NSLog(@"1111111111");
    
    
    // 5> exit 推出当前  , 不要退出主线程
    // 退出后, 线程操作不会在执行
//    [NSThread exit];
    NSLog(@"主:::%@",[NSThread mainThread]);
    
    // 6>线程的优先级 默认 线程优先级都是0.5.
     double priority = [NSThread threadPriority];
    NSLog(@"%lf", priority);
    
    // 7> name 线程名
    [NSThread currentThread].name = @"xxx的线程";
    
    // 8> isMainThread 判断当前线程是否是主线程
    [NSThread isMainThread];
    //  判断线程对象是不是主线程
    [[NSThread currentThread] isMainThread];
    
    
    
    // 9> 初始化方法  .   需要手动管理 开始
    // 初始化一个未启动的线程
   NSThread *myThread = [[NSThread alloc] initWithTarget:self selector:@selector(mythread) object:nil];
    [myThread start];
    
    // 10> 将耗时的操作,放入后台执行
    // 不关心线程问题
    [self performSelectorInBackground:@selector(back) withObject:nil];
    
    // 11> 返回主线程.
//    [self performSelectorOnMainThread:@selector(omMainThreadHandel:) withObject:data waitUntilDone:YES];
    
    
}

- (void)mythread
{
       NSLog(@"###########%s", __FUNCTION__);
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic.dd008.com/source/201602/03/07533622ce.jpg"]];
    
    [self performSelectorOnMainThread:@selector(omMainThreadHandel:) withObject:data waitUntilDone:YES];
    
}

- (void)omMainThreadHandel:(id)object
{
    _imagevie.image = [UIImage imageWithData:object];

}




- (void)newThread
{
    // 3> 延迟当前线程的执行
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:2];
    [NSThread sleepUntilDate:date];
    NSLog(@"1111");
    
    NSThread *thread = [NSThread currentThread];
    thread.name = @"李其昌的线程";
    NSLog(@"newThread : %@", thread);

    double priority = [NSThread threadPriority];
    NSLog(@"%lf", priority);
    
}






- (IBAction)buttonPlay:(id)sender {
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
